package ualrcdg.client;

import android.app.Activity;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

/**
 * This class allows an existing user to log 
 * into the CDG Server to access the files
 * associated with their account.
 * Continues from {@link CreateProfileActivity} or {@link LoginActivity}
 * Continues to {@link GameFetcherActivity}
 */
public class LoginActivity extends Activity {

	protected CDGApplication App;
	private CommunicatorService mBoundService;
    private Boolean mIsBound;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.loginlayout);
        
        final LoginActivity li = this;
        final EditText UsernameField = (EditText) findViewById(R.id.EditUsername);
        final EditText PasswordField = (EditText) findViewById(R.id.EditPassword);
        
        final Button YesButton = (Button) findViewById(R.id.button1);
        YesButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	//
            	//Check Username and Password
            	//TODO communicator.run();
            	String Username = UsernameField.getText().toString();
           	 	String Password = PasswordField.getText().toString();
            	startActivity(new Intent(li, ualrcdg.client.GameFetcherActivity.class).putExtra("Username", Username).putExtra("Password", Password));
            }
        }); 
    }
    private ServiceConnection mConnection = new ServiceConnection() {
        public void onServiceConnected(ComponentName className, IBinder service) {
            mBoundService = ((CommunicatorService.LocalBinder)service).getService();

        }
        public void onServiceDisconnected(ComponentName className) {
            mBoundService = null;
        }
    };

    private void doBindService() {
        bindService(new Intent(LoginActivity.this, CommunicatorService.class), mConnection, Context.BIND_AUTO_CREATE);
        mIsBound = true;
        mBoundService.IsBoundable();
    }


    private void doUnbindService() {
        if (mIsBound) {
            // Detach our existing connection.
            unbindService(mConnection);
            mIsBound = false;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        doUnbindService();
    }
}

